Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Cambiar caso no funciona con URL [JavaScript]

Estoy tratando de usar una declaración de cambio:

 var currentURL = window.location.href; switch(currentURL) { // Home page case "http://localhost/myWebsitePath/": alert("Home!"); break; // Blog home page case "http://localhost/myWebsitePath/blog/": alert("Blog!"); break; }

Cuando visito http://localhost/myWebsitePath/, recibo la alerta. Cuando visito http://localhost/myWebsitePath/blog/, no siempre recibo la alerta. A veces parece funcionar al hacer clic en un hipervínculo desde la página de inicio. ¿Necesito escapar de algún carácter especial para garantizar que funcione?

Nota: ambas rutas tienen sus respectivos archivos index.html en las carpetas

 console.log(currentURL); console.log(typeof(currentURL));

Produce:

http://localhost/myWebsitePath/blog/ string

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parece que ahora funciona correctamente si uso:

 var currentURL = window.location.href; currentURL = encodeURIComponent(currentURL);

Luego cambia de caso de la siguiente manera:

 switch(currentURL) { // Home page case "http%3A%2F%2Flocalhost%2FmyWebsitePath%2F": alert("Home!"); break; // Blog home page case "http%3A%2F%2Flocalhost%2FmyWebsitePath%2Fblog%2F": alert("Blog!"); break; }

Utilicé https://meyerweb.com/eric/tools/dencoder/ para codificar/decodificar las URL inicialmente e insertarlas en mis cajas de cambio.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!